package com.ts.api.module.api.convert.receive.handler;

import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.context.ApiContext;
import com.ts.api.exp.ApiMsgException;
import com.ts.api.module.api.convert.receive.context.ApiReceivePhoneMd5ConvertContext;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveApply;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveDecrypt;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveNotice;
import com.ts.api.module.api.convert.receive.entity.ApiReceivePhoneMd5DataExchange;
import com.ts.api.module.api.entity.ApiUserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  API渠道接入-封装
 *
 *  @author Pan
 */
@Component
public class DelegatingApiReceivePhoneMd5ConvertHandler implements ApiReceivePhoneMd5ConvertHandler {

    @Autowired
    private ApiReceivePhoneMd5ConvertContext apiReceivePhoneMd5ConvertContext;

    @Override
    public Map<String, String> requireParam(RequestVO requestVO) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).requireParam(requestVO);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_REQUIRE_EVENT_ERROR, e);
        }
    }

    @Override
    public ApiReceiveDecrypt decrypt(Map<String, String> paramMap, RequestVO requestVO) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).decrypt(paramMap, requestVO);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_DECRYPT_ERROR, e);
        }
    }

    @Override
    public ApiUserRequest parseDataExchange(ApiReceiveDecrypt apiReceiveDecrypt) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            ApiUserRequest apiUserRequest = apiReceivePhoneMd5ConvertContext.getReceive(clientId).parseDataExchange(apiReceiveDecrypt);
            if (apiUserRequest == null) {
                throw new ApiMsgException(ApiEnum.API_RECEIVE_PARSE_DATA_EXCHANGE_NULL);
            }
            return apiUserRequest;
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_PARSE_DATA_EXCHANGE_ERROR, e);
        }
    }

    @Override
    public Map<String, Object> respDataExchange(ApiReceivePhoneMd5DataExchange dataExchangeMessage) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).respDataExchange(dataExchangeMessage);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_RESP_DATA_EXCHANGE_ERROR, e);
        }
    }

    @Override
    public ApiUserRequest parseApply(ApiReceiveDecrypt apiReceiveDecrypt) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            ApiUserRequest apiUserRequest = apiReceivePhoneMd5ConvertContext.getReceive(clientId).parseApply(apiReceiveDecrypt);
            if (apiUserRequest == null) {
                throw new ApiMsgException(ApiEnum.API_RECEIVE_PARSE_APPLY_NULL);
            }
            return apiUserRequest;
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_PARSE_APPLY_ERROR, e);
        }
    }

    @Override
    public Map<String, Object> respApply(ApiReceiveApply applyMessage) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).respApply(applyMessage);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_RESP_APPLY_ERROR, e);
        }
    }

    @Override
    public void fieldMapping(ApiUserRequest apiUserRequest) {
        //  do nothing
    }

    @Override
    public Object reqPullApplyNotice(ApiReceiveNotice apiReceiveNotice) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).reqPullApplyNotice(apiReceiveNotice);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_RESP_APPLY_ERROR, e);
        }
    }

    @Override
    public boolean respPullApplyNotice(String responseBody) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).respPullApplyNotice(responseBody);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_RESP_APPLY_ERROR, e);
        }
    }

    @Override
    public Object reqPushApplyNotice(ApiReceiveNotice apiReceiveNotice) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).reqPushApplyNotice(apiReceiveNotice);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_RESP_APPLY_ERROR, e);
        }
    }

    @Override
    public boolean respPushApplyNotice(String responseBody) {
        try {
            String clientId = ApiContext.CLIENT_ID.get();
            return apiReceivePhoneMd5ConvertContext.getReceive(clientId).respPushApplyNotice(responseBody);
        } catch (ApiMsgException apiMsgException) {
            throw apiMsgException;
        } catch (Exception e) {
            throw new ApiMsgException(ApiEnum.API_RECEIVE_RESP_APPLY_ERROR, e);
        }
    }
}
